package com.may.copy.zero;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

/**
 * @program: com.may.netty
 * @description:
 * @author: Bobby.Ma
 * @create: 2020-09-20 11:20
 **/
public class NIOClient {
    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.connect(new InetSocketAddress("127.0.0.1",9999));
        //得到文件 channel
        FileChannel channel = new FileInputStream("C:\\Users\\19167\\Desktop\\nginx-1.18.0.zip").getChannel();
        long start = System.currentTimeMillis();
        //在 linux 下，一次调用 transferTo 方法就可以发送整个文件
        //在 windows 下，一次调用 transferTo 方法只能发送 8M 文件，所以需要分段发送，而且要注意传输时位置
        long total = channel.transferTo(0, channel.size(), socketChannel);//底层使用到零拷贝
        long end = System.currentTimeMillis();
        System.out.println("发送总字节数：" + total + "，耗时：" + (end - start) + "毫秒");

        channel.close();
    }
}
